package cn.kgc.ebookdemo02.controller;

import cn.kgc.ebookdemo02.entity.ResultEntity;
import cn.kgc.ebookdemo02.pojo.EbookEntry;
import cn.kgc.ebookdemo02.service.EbookEntryService;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author shkstart
 * @create 2020-05-19 15:07
 */

@RestController
public class EntityController {

    @Resource
    EbookEntryService ebookEntryService;



    @RequestMapping("/selectEntry")
    public ResultEntity<PageInfo<EbookEntry>> selectEntry(@RequestParam(value = "categoryId", required = false, defaultValue = "0") Integer categoryId,
                                                          @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                                                          @RequestParam(value = "pageSize", required = false, defaultValue = "2") Integer pageSize) {
        PageInfo<EbookEntry> pageInfo = ebookEntryService.select(pageNum, pageSize, categoryId);
        return ResultEntity.successWithoutData(pageInfo);
    }

    //前端post请求，参数是json必须添加@RequestBody
    //添加和修改
    @RequestMapping("/saveEntry")
    public ResultEntity<Integer> saveEntry(@RequestBody EbookEntry ebookEntry) {
        Integer integer = 0;
        if (ebookEntry.getId() == null) {
            integer = ebookEntryService.add(ebookEntry);
        } else {
            integer = ebookEntryService.update(ebookEntry);
        }

        return ResultEntity.successWithoutData(integer);
    }
    //根据id查询
    @RequestMapping("/getEntryId")
    public ResultEntity<EbookEntry> getEntryId(Integer id) {
        EbookEntry entry = ebookEntryService.selectById(id);
        return ResultEntity.successWithoutData(entry);
    }

    //根据id删除
    @RequestMapping("/delEntryId")
    public ResultEntity<Integer> delEntryId(Integer id) {
        Integer integer = ebookEntryService.delete(id);
        return ResultEntity.successWithoutData(integer);
    }

}
